package com.kfm.base.t;

import java.io.*;

public class TestSerializable {

    public static void main(String[] args) throws CloneNotSupportedException {

        String jsonString = "{key:value, key2:value2, key3:value3}";
        // fastjson  jackson2  gson

        GirlFriend gf = new GirlFriend("韩月", 20, "BMW");
        Person qs = new Person("秦爽", 21, '男', gf);

        String str = "{name: '秦爽', age: 21, gender: '男', girlFriend: { name: '韩月', age: 20, car: 'BMW'}}";
        System.out.println(str);
    }

    private static void extracted1() {
        GirlFriend gf = new GirlFriend("韩月", 20, "BMW");

        Person qs = new Person("秦爽", 21, '男', gf);

        try ( ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
              // 序列化
              ObjectOutputStream oos = new ObjectOutputStream(baos);
              ) {

            oos.writeObject(qs);

            byte[] byteArray = baos.toByteArray();

            System.out.println(new String(byteArray));


            ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
            // 反序列化
            ObjectInputStream ois = new ObjectInputStream(bis);

            Object object = ois.readObject();

            if (object instanceof Person person) {
                qs.setName("秦爽爽");

                gf.setName("韩月月");
                System.out.println("old:" + qs);
                System.out.println("new: " + person);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private static void extracted() throws CloneNotSupportedException {
        GirlFriend gf = new GirlFriend("韩月", 20, "BMW");

        Person qs = new Person("秦爽", 21, '男', gf);

        System.out.println(qs);

        Object clone = qs.clone();

        System.out.println(clone);


        System.out.println(qs == clone);

        qs.setName("秦爽爽");

        gf.setName("韩月月");

        System.out.println(qs);
        System.out.println(clone);
    }
}
